package hr.bean;

import hr.dao.EmployeeDAO;

import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import resources.pojo.hr_branch;
import resources.pojo.hr_department;
import resources.pojo.hr_designation;
import resources.pojo.hr_employee;
import resources.util.Tool;

@ManagedBean(name="employee")
@SessionScoped
public class Employee implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	String message;
	public boolean flagInsert = true;
	private ArrayList<hr_employee> listEmployee;
	private Integer[] pages;
	private String namesearch;
	private int currentPage;
	private hr_employee staff_add;
	private hr_employee staff_update = null; // update staff
	
	public void onloadAddNew(){
		if(!Tool.isPostBack()){
			initObject();
		}
	}
	
	public void initObject(){
		message = "";
		staff_add = new hr_employee();
		staff_add.setDepartment(new hr_department());
		staff_add.setDesignation(new hr_designation());
		staff_add.setBranch(new hr_branch());
		flagInsert = true;
	}
	
	public void onloadUpdate(){
		if(staff_update == null){
			Tool.redirect("employee.html");
		}
	}
	
	public void onloadManage(){
		if(!Tool.isPostBack()){
			initManage();
		}
	}
	
	public void initManage(){
		listEmployee = new ArrayList<hr_employee>();
		listEmployee.addAll(EmployeeDAO.getListEmployee("", 1, Tool.max_row));
		pages = Tool.getPages();
	}
	
	public void paging(){
		listEmployee.addAll(EmployeeDAO.getListEmployee("", currentPage, Tool.max_row));
		pages = Tool.getPages();
	}
	
	public void clickToEdit(ActionEvent evt){
		try{
			hr_employee object = (hr_employee)evt.getComponent().getAttributes().get("object");
			if(object != null){
				staff_update = object;
			}
			Tool.redirect("update-employee.html");
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert(){
		try{
			if(flagInsert){
				if(staff_add.getAddress_permanent().length() > 100){
					Tool.message("Address permanent is too long (100 characters)");
					return;
				}else if(staff_add.getAddress_current().length() > 100){
					Tool.message("Address current is too long (Max is 100 characters)");
					return;
				}else if(staff_add.getFamily().length() > 500){
					Tool.message("Address permanent is too long (Max is 500 characters)");
					return;
				}
				staff_add.setId("EMP" + Tool.generateID());
				staff_add.setStatus(1);
				boolean flag = EmployeeDAO.action("insert",staff_add);
				if(flag){
					message = "Insert is completed";
					Tool.message(message);
					flagInsert = false;
					Tool.redirect("employee.html");
				}else{
					message = "Cannot insert";
					Tool.message(message);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void update(){
		try{
			boolean flag = EmployeeDAO.action("update",staff_update);
			if(flag){
				message = "Update is completed";
				Tool.message(message);
				staff_update = null;
				Tool.redirect("employee.html");
			}else{
				message = "Cannot update";
				Tool.message(message);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setListEmployee(ArrayList<hr_employee> listEmployee) {
		this.listEmployee = listEmployee;
	}

	public ArrayList<hr_employee> getListEmployee() {
		return listEmployee;
	}

	public void setStaff_add(hr_employee staff_add) {
		this.staff_add = staff_add;
	}

	public hr_employee getStaff_add() {
		return staff_add;
	}

	public void setStaff_update(hr_employee staff_update) {
		this.staff_update = staff_update;
	}

	public hr_employee getStaff_update() {
		return staff_update;
	}

	public void setPages(Integer[] pages) {
		this.pages = pages;
	}

	public Integer[] getPages() {
		return pages;
	}

	public void setNamesearch(String namesearch) {
		this.namesearch = namesearch;
	}

	public String getNamesearch() {
		return namesearch;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}
}
